package fr.univnantes.lina.ftbcleaner;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import fr.univnantes.lina.ftbcleaner.FTBPropertiesSingleWordCollector;
import fr.univnantes.lina.ftbcleaner.MultiWordDesambiguationCase;
import fr.univnantes.lina.ftbcleaner.WordForms;
import fr.univnantes.lina.ftbcleaner.WordLemma;
import fr.univnantes.lina.javautil.CollectionUtilities;


public class FTBPropertiesSingleWordCompiler extends
		FTBPropertiesSingleWordCollector {

	public FTBPropertiesSingleWordCompiler() {
		super();
	}

	/**
	 * After having collected information coming from distinct occurrences of MW 
	 * and collected information from SW, generalize the information
	 * TODO en a t on vraiment l'utilité ?
	 */
	public void compilePropertiesFromSingleWords(Map<String,WordLemma> multiWordsExpressionHashMap, WordForms aWordForms) {
	
		/*	Map<String, Map<String, Integer>> word2CatMap = aWordOccurrences.getWord2CatMap();
		Map<String, Map<String, Integer>> wordCat2SubcatMap  = aWordOccurrences.getWordCat2SubcatMap();
		Map<String, Map<String, Integer>> wordCat2LemmaMap  = aWordOccurrences.getWordCat2LemmaMap();
		Map<String, Map<String, Integer>> wordCat2MphMap  = aWordOccurrences.getWordCat2MphMap();
		 */
		Iterator<String> multiWordsExpressionHashMapKeyIterator = 	multiWordsExpressionHashMap.keySet().iterator();
	
		// for each mw lemma
		while (multiWordsExpressionHashMapKeyIterator.hasNext()) {
			WordLemma aMWE = multiWordsExpressionHashMap.get(multiWordsExpressionHashMapKeyIterator.next());
	
			// If the current MW has empty catints
			if (aMWE.getNumberOfNullCatintValue() >0) {
				// if it exists at least a sw sequence
				// for each whole form, provide the corresponding cat or the subcat or the mph 
				// the problem is to associate  the cat with the subcat and the mph
				// the solution could be a Map from the word+cat to ...
				// TODO ? process each MW later online or here
	
				// for each mw form
				Set<String> wordsSet = aMWE.getWordsMap().keySet();
				Iterator<String> wordsIterator = wordsSet.iterator();
	
				// For each MW form
				while (wordsIterator.hasNext()) {
					String wordsString = wordsIterator.next();
					String wordArray[] = wordsString.split(" ");
	
					// if it has the sufficient size
					if (aMWE.getSize() == wordArray.length) {
	
						//String features = "";
						// for each single word of the 
						String catSeq = "";
						String subcatSeq = "";
						String lemmaSeq = "";
						String mphSeq = "";
	
						Boolean undefinedCatintCase = false;
						Boolean undefinedSubcatCase = false;
						Boolean undefinedLemmaCase = false;
						Boolean undefinedMphCase = false;
	
						for (int l = 0; l <wordArray.length ; l++ ) {
	
							if (isTheFirstFormMoreSalientThanOthersBasedOnFBHeuristic(aWordForms.getWordFormCatCounter(wordArray[l]).getCatMap())) 
								catSeq += " "+CollectionUtilities.getTheFirstFormOfASortedMap((TreeMap<String, Integer>) aWordForms.getWordFormCatCounter(wordArray[l]).getCatMap());
							else undefinedCatintCase = true ;// catSeq += " U";
							if (!undefinedCatintCase) {
								if (isTheFirstFormMoreSalientThanOthersBasedOnFBHeuristic(aWordForms.getWordCatFormOtherCounters(wordArray[l]+" "+catSeq).getSubcatMap())) 
									subcatSeq += " "+CollectionUtilities.getTheFirstFormOfASortedMap((TreeMap<String, Integer>) aWordForms.getWordCatFormOtherCounters(wordArray[l]+" "+catSeq).getSubcatMap());
								else undefinedSubcatCase = true ;//subcatSeq += " U";
								if (isTheFirstFormMoreSalientThanOthersBasedOnFBHeuristic(aWordForms.getWordCatFormOtherCounters(wordArray[l]+" "+catSeq).getLemmaMap())) 
									lemmaSeq += " "+CollectionUtilities.getTheFirstFormOfASortedMap((TreeMap<String, Integer>) aWordForms.getWordCatFormOtherCounters(wordArray[l]+" "+catSeq).getLemmaMap());
								else undefinedLemmaCase = true ; // lemmaSeq += " U";
								if (isTheFirstFormMoreSalientThanOthersBasedOnFBHeuristic(aWordForms.getWordCatFormOtherCounters(wordArray[l]+" "+catSeq).getMphMap())) 
									mphSeq += " "+CollectionUtilities.getTheFirstFormOfASortedMap((TreeMap<String, Integer>) aWordForms.getWordCatFormOtherCounters(wordArray[l]+" "+catSeq).getMphMap());
								else undefinedMphCase = true ; // mphSeq += " U";
							}
							else {
								undefinedSubcatCase = true ;
								undefinedLemmaCase = true ; 
								undefinedMphCase = true ; 
							}
	
						}
						// TODO revoir cette partie en commentaire
						/*
						if (!undefinedCatintCase) {
							catSeq = catSeq.trim();
							aMWE.addMostProbableCatintSequence(wordsString, catSeq);
						}
	
						if (!undefinedSubcatCase) {
							subcatSeq = subcatSeq.trim();
							aMWE.addMostProbableSubcatSequence(wordsString, subcatSeq);
	
						}
						if (!undefinedLemmaCase) {
							lemmaSeq = lemmaSeq.trim();
							aMWE.addMostProbableLemmaSequence(wordsString, lemmaSeq);
	
						}
						if (!undefinedMphCase) {
							mphSeq = mphSeq.trim();
							aMWE.addMostProbableMphSequence(wordsString, mphSeq);
	
						}
						*/
						//System.err.println("Debug: currentWordFormOcc >"+wordsString+"<");
						if (!undefinedCatintCase && !undefinedSubcatCase && !undefinedLemmaCase && !undefinedMphCase ) {
	
							/*						//features = features.trim();
						if (aMWE.getMostProbableMphSequence().keySet().isEmpty()) {
							numberOfMWWithAtLeastOneCorrespondingSWSeq++;
							numberOfMWOccurrencesCorrespondingToASWSeq += aMW.getOccurrence();
						}
						//aMW.addFeaturesFromSWSeq(features);
						if (aMWE.getMostProbableMphSequence().keySet().size() > 1) {
							numberOfMWWithSeveralSWSeqFeaturesSet.add(wordsString);
						}
	
						if (aMWE.getDesambiguisationCase() != -1) numberOfAlreadyDesambiguatedCaseCorrespondingToASWSeq++;
						if (aMWE.getNumberOfNullCatintValue() >0)*/ 
							aMWE.setDesambiguisationCase(MultiWordDesambiguationCase.MWWiIndependentFeaturesComputedFromSW);
							//System.out.println("Debug: MATCHED "+wordsString+ " cats "+aMW.getCatsFromSWSeqSet().toString());
						}
					}
				} 
			}
		}
	
	}

}